package com.kqb.auth.controller;

import cn.hutool.core.util.ObjectUtil;
import com.kqb.api.auth.bo.UserInfoInTokenBO;
import com.kqb.auth.domain.dto.AuthenticationDTO;
import com.kqb.auth.service.AuthAccountService;
import com.kqb.auth.utils.JwtUtils;
import com.kqb.common.core.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杉果树皮
 * @date 2025/4/11 10:14
 * @description:
 */
@RestController
@Api(tags = "登录")
@RequestMapping("/auth")
public class LoginController {
    @Autowired
    AuthAccountService authAccountService;
    @Autowired
    JwtUtils jwtUtils;

    @PostMapping("/login")
    @ApiOperation("登录")
    public AjaxResult login(@RequestBody AuthenticationDTO authenticationDTO) {
        AjaxResult userInfoResult = authAccountService.getUserInfoByNameAndPassword(authenticationDTO);
        UserInfoInTokenBO userInfoInTokenBO = (UserInfoInTokenBO) userInfoResult.get("data");

        // 1. 判断是否成功
        if (ObjectUtil.isNull(userInfoInTokenBO)) {
            return userInfoResult;
        }
        // 2. 返回token
        return AjaxResult.success(jwtUtils.generateToken(userInfoInTokenBO));
    }

}
